package com.qire.manhua.controller;

import android.view.View;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.TeensModeManage;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.activity.BuyCoinsMarketDialogActivity;
import com.qire.manhua.databinding.ActivityBuyCoinsMarketDialogBinding;
import com.qire.manhua.databinding.LayoutBuyCoinsMarketDialogItemBinding;
import com.qire.manhua.dialog.PayWaySelectorOfBuyCoinDialog;
import com.qire.manhua.dialog.TeensPasswordAuthorizeDialog;
import com.qire.manhua.model.entity.MarketInfo.WaresEntity;
import com.qire.manhua.model.enumeration.PaymentMethod;
import com.qire.manhua.model.enumeration.TeensModeScene;
import com.qire.manhua.model.viewModel.impl.BuyCoinsMarketDialogViewModel;
import com.qire.manhua.model.viewModel.impl.BuyCoinsMarketDialogViewModel.WaresItemViewModel;

import androidx.annotation.NonNull;

public class BuyCoinsMarketDialogController extends EventController<BuyCoinsMarketDialogActivity> implements AutoVariable<LayoutBuyCoinsMarketDialogItemBinding> {

    private BuyCoinsMarketDialogViewModel viewModel;

    public BuyCoinsMarketDialogController(@NonNull BuyCoinsMarketDialogActivity owner) {
        super(owner);
    }

    @Override
    protected void bindView() {
        ActivityBuyCoinsMarketDialogBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setBuyCoinsMarketDialogController(this);
    }

    @Override
    public void bindVariable(LayoutBuyCoinsMarketDialogItemBinding viewBind) {
        viewBind.setBuyCoinsMarketDialogController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(BuyCoinsMarketDialogViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    /** 关闭弹窗 */
    public void closeBtnOnClick(View view) {
        owner.finish();
    }

    /** 商品选项单击事件 */
    public void waresLayoutOnClick(View view) {
        WaresItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), WaresItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            itemViewModel.toggle();
        }
    }

    /**
     * 立即支付按钮：
     * 1.判断是否有选择好充值金额;
     * 2.打开支付方式窗口;
     * 3.选择支付方式窗口后回调通知支付渠道：payChannel。
     */
    public void paymentBtnOnclick(View view) {
        final WaresEntity waresEntity = viewModel.selectedWares();

        if (ObjectUtil.isNull(waresEntity)) {
            ToastHelper.showSnackBar("请先选择充值金额", Prompt.WARNING);
            return;
        }

        //在打开支付方式选择弹窗时希望隐藏套餐面板弹窗，不应该使用下面的这种方式，因为在小米手机上经过MIUI优化的弹窗可能存在一起被GONE掉，无法显示出来的情况。
        //应该更精准的使用窗体根View的隐藏。
//        activity.getWindow().getDecorView().setVisibility(View.GONE);

        if(TeensModeManage.teensModeEnable()) {
            new TeensPasswordAuthorizeDialog(view.getContext(), TeensModeScene.Recharge, () -> showPayWaySelectorOfBuyCoinDialog(waresEntity)).show();
            return;
        }

        showPayWaySelectorOfBuyCoinDialog(waresEntity);
    }

    private void showPayWaySelectorOfBuyCoinDialog(WaresEntity waresEntity) {
        PayWaySelectorOfBuyCoinDialog selectorOfBuyCoinDialog = new PayWaySelectorOfBuyCoinDialog(owner, this::paymentMethodOnClick)
                .setWaresFee(String.valueOf(waresEntity.fee))
                .setWaresDescribe(waresEntity.getDescribe());
        // 因为前面是把当前activity隐藏，所以这里支付选项对话框cancel也要把隐藏的activity关闭
        selectorOfBuyCoinDialog.setOnCancelListener(dialog -> owner.finish());
        selectorOfBuyCoinDialog.show();
    }

    public void paymentMethodOnClick(View view) {
        PaymentMethod paymentMethod = ObjectUtil.as(view.getTag(), PaymentMethod.class);
        if(ObjectUtil.nonNull(paymentMethod)) {
            viewModel.createOrder(paymentMethod, owner.getClass().getSimpleName());
        }
    }


}
